Skip to content

Extract early config validation from runServeCmd into testable helpers#45583

Open
raju249 wants to merge 1 commit into
fleetdm:mainfrom
raju249:33370-serve-config-validation-tests
Open

Extract early config validation from runServeCmd into testable helpers#45583
raju249 wants to merge 1 commit into
fleetdm:mainfrom
raju249:33370-serve-config-validation-tests

Conversation

@raju249
Copy link
Copy Markdown
Contributor

@raju249 raju249 commented May 15, 2026

Extracts five pure config validations from runServeCmd into helpers in a new cmd/fleet/serve_validation.go, and adds table-driven unit tests for each:

  • validateOTELLoggingConfig — OTEL logs requires tracing enabled
  • validateOsqueryHostIdentifier — must be one of provided, instance, uuid, hostname
  • normalizeAndValidateServerURLPrefix — trims/normalizes leading-slash, validates against allowedURLPrefixRegexp
  • validateServerPrivateKeyExclusiveprivate_key and private_key_arn are mutually exclusive
  • validateServerPrivateKeyLength — minimum 32 bytes when set

Behavior is byte-identical: error messages preserved, runServeCmd still calls initFatal with the same descriptions at the same points. serve.go shrinks by 21 lines.

Broader plan

Issue #33370 calls for moving logic out of serve.go ("should only contain critical config and dependency injection logic"). This PR is one slice. Realistic follow-ups, each in their own small PR:

  • Extract more config validations (Apple APNs/SCEP both-or-neither, etc.)
  • Use the initFatal injection from Inject initFatal as a package-level var (#33370) #45343 to cover runtime failure paths (datastore init, Redis init, MDM init, etc.)
  • Larger extractions (license init, MDM wiring, mailer init)

Related issue: Refs #33370

Checklist for submitter

  • Added/updated automated tests
  • Input data is properly validated (helpers added, no SQL/JS/shell paths involved)
  • Changes file: not applicable — internal refactor with no user-visible behavior change

Summary by CodeRabbit

  • Refactor

    • Improved server startup configuration validation with modular helper functions for better maintainability.
  • Tests

    • Added comprehensive unit tests for server configuration validation logic, covering private key settings, osquery identifiers, OTEL logging configuration, and URL prefix normalization.

Review Change Stack

Move five pure config validations (OTEL logs/tracing dependency, osquery host identifier, server URL prefix normalization, private key flag exclusivity, private key length) into helper functions in cmd/fleet/serve_validation.go and add unit tests for each. Behavior is unchanged; runServeCmd still fatals at the same points via the same initFatal descriptions. Part of fleetdm#33370.
@raju249 raju249 requested a review from a team as a code owner May 15, 2026 12:47
Copy link
Copy Markdown

@claude claude Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 15, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 230a2548-b5f4-4cb0-80a8-ba47fa3f4e34

📥 Commits

Reviewing files that changed from the base of the PR and between 2c47f8e and eb459f6.

📒 Files selected for processing (3)
  • cmd/fleet/serve.go
  • cmd/fleet/serve_validation.go
  • cmd/fleet/serve_validation_test.go

Walkthrough

This PR consolidates server startup configuration validation by extracting inline validation logic from cmd/fleet/serve.go into dedicated helper functions in a new file cmd/fleet/serve_validation.go. Five validation helpers enforce server private key constraints (mutual exclusivity and minimum length), restrict osquery host identifier to an allowlist, validate OTEL logging dependencies, and normalize/validate server URL prefix. The refactored serve.go delegates to these helpers at three validation checkpoints, and comprehensive table-driven tests cover all validation scenarios and edge cases.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 54.55% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: extracting configuration validation logic from runServeCmd into testable helper functions.
Description check ✅ Passed The description covers the five validations extracted, explains behavior is identical, notes the shrinking of serve.go, references the related issue, and marks the applicability of checklist items appropriately.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@raju249
Copy link
Copy Markdown
Contributor Author

raju249 commented May 15, 2026

@getvictor - I did some more refactoring of serve.go file. Can you please take a review?

Thanks! 🙏 🙇

@codecov
Copy link
Copy Markdown

codecov Bot commented May 15, 2026

Codecov Report

❌ Patch coverage is 75.00000% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 66.73%. Comparing base (c19df6d) to head (eb459f6).
⚠️ Report is 12 commits behind head on main.

Files with missing lines Patch % Lines
cmd/fleet/serve.go 0.00% 10 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #45583      +/-   ##
==========================================
- Coverage   66.74%   66.73%   -0.01%     
==========================================
  Files        2740     2741       +1     
  Lines      219163   219178      +15     
  Branches    10947    10947              
==========================================
- Hits       146283   146277       -6     
- Misses      59649    59677      +28     
+ Partials    13231    13224       -7     
Flag Coverage Δ
backend 68.58% <75.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@getvictor getvictor self-assigned this May 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants